package com.distribute.framework.core.web.helper;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.distribute.framework.core.xwork.WebContext;

/**
 * 关于cookie的操作
 * domain，表示cookie的有效域，如希望子域名跨域，domain则为".主域名",前面加一点为通配符
 * @author uiu
 *
 */
public class CookieHelper {
	
	public final static int ONE_DAY=60*60*24;//一天
	public final static int ONE_MONTH=60*60*24*30;//一个月
	public final static int ONE_WEEK=60*60*24*7;//一星期
	public final static int ONE_YEAR=60*60*24*7;//一年	
	public final static int ONE_TIME=-1;//一次(浏览器退出后就没了)
	
	/**
	 * 取COOKIE
	 */
	public static Cookie getCookie(String name)
	{
		HttpServletRequest request = WebContext.getServletRequest();
		Cookie[] cookies = request.getCookies();		
		if(cookies!=null)
			for(Cookie cookie:cookies){
				//System.out.println(cookie.getDomain()+" - "+cookie.getPath()+" - "+cookie.getName()+" - "+cookie.getValue());
				if(equals(cookie.getName(),name))
					return cookie;
			}
		return null;
	}
	
	/**
	 * 取COOKIE值
	 */
	public static String getCookieValue(String name)
	{
		Cookie cookie=getCookie(name);
		if(cookie!=null)
			return cookie.getValue();
		return null;
	}
	
	/**
	 * 设置cookie
	 * @param domain
	 * @param path
	 * @param key
	 * @param value
	 * @param time 有效时间（单位秒） 0表示立即删除  -1表示浏览器关掉后失效
	 */
	public static void setCookie(String domain,String path,String name,String value,int time)
	{
		HttpServletResponse response = WebContext.getServletResponse();
		Cookie cookie = new Cookie(name,value);
		cookie.setMaxAge(time);//默认cookie保存1个月
		cookie.setPath(path);//路径
		if(!empty(domain))
			cookie.setDomain(domain);
		response.addCookie(cookie);
	}
	
	/**
	 * 删除cookie
	 * @param key
	 */
	public static void delCookie(String domain,String path,String name)
	{
		HttpServletResponse response = WebContext.getServletResponse();
		HttpServletRequest request = WebContext.getServletRequest();
		Cookie[] cookies = request.getCookies();
		if(cookies!=null)
			for(Cookie cookie:cookies){
				if(equals(cookie.getName(),name))
				{
					if(!empty(domain))
						cookie.setDomain(domain);
					cookie.setPath(path);
					cookie.setMaxAge(0);
					response.addCookie(cookie);	
				}
			}
	}
	
	/**
	 * 清空cookie
	 */
	public static void clear(String domain,String path) {
		if(domain==null)domain="";
		HttpServletResponse response = WebContext.getServletResponse();
		HttpServletRequest request = WebContext.getServletRequest();
		Cookie[] cookies = request.getCookies();
		if(cookies!=null)
			for(Cookie cookie:cookies){
				if(!empty(domain))
					cookie.setDomain(domain);
				cookie.setPath(path);
				cookie.setMaxAge(0);				
				response.addCookie(cookie);	
			}
	}
	
	public static Boolean equals(String v1,String v2)
	{
		if(v1==null && v2==null)
			return true;
		if(v1!=null && v2!=null && v1.equals(v2))
			return true;
		else
			 return false;
	}
	
	public static Boolean empty(String string)
	{
		if(string==null || string.trim().equals(""))
			return true;
		return false;		
	}
	
}